#include "i2cled.h"
int init_led = 0;
int step_led = 0;

int set_i2c_register(int file, unsigned char addr, unsigned char reg, unsigned char value)
{
    unsigned char outbuf[2];
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[1];

    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = outbuf;
    outbuf[0] = reg;
    outbuf[1] = value;
    packets.msgs  = messages;
    packets.nmsgs = 1;
    if(ioctl(file, I2C_RDWR, &packets) < 0){
        perror("Unable to send data");
        return 1;
    }
    return 0;
}


int get_i2c_register(int file, unsigned char addr, unsigned char reg, unsigned char *val)
{
    unsigned char inbuf, outbuf;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];

    /*
     * In order to read a register, we first do a "dummy write" by writing
     * 0 bytes to the register we want to read from.  This is similar to
     * the packet in set_i2c_register, except it's 1 byte rather than 2.
     */
    outbuf = reg;
    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = &outbuf;
    /* The data will get returned in this structure */
    messages[1].addr  = addr;
    messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
    messages[1].len   = sizeof(inbuf);
    messages[1].buf   = &inbuf;
    /* Send the request to the kernel and get the result back */
    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0){
        perror("Unable to send data");
        return 1;
    }
    *val = inbuf;
    return 0;
}

int current_set(int level,int openflag)
{
    static int init = 0;
    if(level > 10)
       return -1;
    int i2c_file;
	char i2cDevName[15] = {0};
    sprintf(i2cDevName,"/dev/i2c-%d",0);
    if((i2c_file = open(i2cDevName, O_RDWR)) < 0)
		return -1;
    if(init == 0)
    {
        init = 1;
        set_i2c_register(i2c_file, 0x64, 0x06, 0x81);       //Software RESET
    }
    if(!openflag)
    {
        set_i2c_register(i2c_file, 0x64, 0x1, 0x00);        //关闭   0X20
        set_i2c_register(i2c_file, 0x64, 0x2, 0x9E);        //0xFE,Configuration Register (0x02)
        set_i2c_register(i2c_file, 0x64, 0x3, 0x00);        //关闭闪光灯模式
    }
    else
    {
        set_i2c_register(i2c_file, 0x64, 0x1, 0x02);        //开启手电筒模式   0X22
        set_i2c_register(i2c_file, 0x64, 0x2, 0x9E);        //0xFE,Configuration Register (0x02)
        set_i2c_register(i2c_file, 0x64, 0x3, 0x00);        //关闭闪光灯模式
        set_i2c_register(i2c_file, 0x64, 0x4, level*(step_led+1) + 4*(init_led+1)); //电流寄存器:0x00=2.4mA,0x15=64mA,1约为3mA
    }

    close(i2c_file);
    return 0;
}

//========================================================================================================
//读取压力传感器的压力值;   i2c_dev=iic编号0-7,val为返回的结果值 单位为:KPa;
//========================================================================================================
int Read_Pressure_Sensor(unsigned char i2c_dev,unsigned char *val)
{

    int i2c_file;
    char i2cDevName[15] = {0};
    sprintf(i2cDevName,"/dev/i2c-%d",i2c_dev);
    if((i2c_file = open(i2cDevName, O_RDWR)) < 0)
        return -1;

    unsigned char Soc_flag = 1;
    unsigned char mesure_statu;
    unsigned char SensorID = 0x6D;
    unsigned char Pressur_H,Pressur_M,Pressur_L,Pressur;
    float         Pressur_ADC;

    //单次压力与温度组合测量
    set_i2c_register(i2c_file, SensorID, 0x30, 0x0A);

    while(Soc_flag > 0) //bit3=1 测量中,bit3=0测量完成 跳出循环
    {
        get_i2c_register(i2c_file, SensorID, 0x30, mesure_statu);
        Soc_flag = mesure_statu & 0x08;
    }

    //压力传感器的地址为:7bit=0x6D,  8bit写0xDA,8bit读为0xDB;
    get_i2c_register(i2c_file, SensorID, 0x06, Pressur_H);
    get_i2c_register(i2c_file, SensorID, 0x07, Pressur_M);
    get_i2c_register(i2c_file, SensorID, 0x08, Pressur_L);

    Pressur_ADC = Pressur_H * 65536 + Pressur_M * 256 + Pressur_L;

    if(Pressur_ADC > 8388608)                                               //超过8388608为负压,需要做正负号处理
        Pressur = (unsigned char)((Pressur_ADC - 16777216) / 64 ) / 1000;   //结果值为KPA,量程为100kpa
    else
        Pressur = (unsigned char)(Pressur_ADC / 64 ) / 1000;                //结果值为KPA       

    *val = Pressur; 

    close(i2c_file);
    
    return 0;
}



